package servlet;


import dao.UserDao;
import entity.User;
import org.apache.ibatis.session.SqlSession;
import util.SqlSessionFactoryUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/logon")
public class LogonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String tel = request.getParameter("tel");
        String sex= request.getParameter("sex");
        User user = new User(username,password,tel,sex);
        SqlSession sqlSession= SqlSessionFactoryUtil.getSqlSession();
        UserDao userDao= sqlSession.getMapper(UserDao.class);
        int num= userDao.logon(user);
        if (num>0){
            out.println("<script>alert('注册成功')</script>");
        }else {
            response.sendRedirect("logon.html");
        }
        sqlSession.commit();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
